package com.avic.common.util;

import com.avic.common.config.properties.CommonProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.File;
import java.net.HttpURLConnection;
import java.util.UUID;

/**
 * @Author huangsheng on 2018/11/26
 */
@Slf4j
@Component
public class FileUploadUtil {

    public static final String UEDITOR_MODULE = "ueditor/";

    public static final String FILE_MODULE = "file/";

    @Autowired
    private CommonProperties commonProperties;

    /**
     * 文件上传处理
     *
     * @param file
     * @return
     */
    public String writeUploadFile(MultipartFile file, String module) {
        // 获取文件名
        String originalFilename = file.getOriginalFilename();
        log.info("上传的文件名为：" + originalFilename);
        // 获取文件的后缀名
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        log.info("上传的后缀名为：" + suffixName);
        if (StringUtils.isBlank(suffixName)) {
            suffixName = ".jpg";
        }
        // 文件上传后的路径
        String filePath = commonProperties.getUploadLocation();
        // 解决中文问题，liunx下中文路径，图片显示问题
        String fileName = module + UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            return commonProperties.getViewFilePrefix() + fileName;
        } catch (Exception e) {
            log.error("上传图片发生错误 {}", e.getMessage());
        }
        return StringUtils.EMPTY;
    }


    /**
     * 通过流操作获取图片链接后缀
     *
     * @return 文件后缀
     */
    public String getHttpFileSuffixByInputStream(BufferedInputStream inputStream) {
        try {
            String contentType = HttpURLConnection.guessContentTypeFromStream(inputStream);
            log.info("获取到contentType = {}", contentType);
            int pos = contentType.indexOf("/");
            if (pos >= 0) {
                String fileSuffix = contentType.substring(pos + 1, contentType.length());
                log.info("获取到文件后缀 = {}", fileSuffix);
                return fileSuffix;
            }
        } catch (Exception e) {
            log.info("通过流操作获取图片链接后缀失败：{}", e);
            return null;
        }
        return null;
    }

    /**
     * 解决win10与win7的ie上传文件得到文件名的问题
     *
     * @param fileName
     * @return
     */
    public static String getWindos(String fileName) {
        if (fileName.indexOf("\\") > -1) {
            return fileName.substring(fileName.lastIndexOf('\\') + 1);
        } else if (fileName.indexOf("/") > -1) {
            return fileName.substring(fileName.lastIndexOf("/") + 1);
        }
        return fileName;
    }
}
